package com.mo.tools.enums;

import com.google.common.collect.Maps;
import org.apache.commons.compress.utils.Lists;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import java.lang.reflect.Field;
import java.util.*;

public interface EnumList {

    @Component
    class EnumClass implements CommandLineRunner {

        private Object value;
        private String label;

        public Object getValue() {
            return value;
        }

        public void setValue(Object value) {
            this.value = value;
        }

        public String getLabel() {
            return label;
        }

        public void setLabel(String label) {
            this.label = label;
        }

        @Override
        public void run(String... args) throws Exception {
            initEnumList();
        }
    }

    Map<String, List<EnumClass>> enumMap = Maps.newHashMap();

    static List<EnumClass> getEnumList(String enumCode) {
        if (ObjectUtils.isEmpty(enumCode)) {
            return new ArrayList<>();
        }
        return Optional.ofNullable(enumMap.get(enumCode)).orElseGet(ArrayList::new);
    }

     static void initEnumList() {
        // 加载枚举class
        Class<EnumListConstant> enumListConstantClass = EnumListConstant.class;
        EnumListConstant enumListConstant = new EnumListConstant();
        Field[] declaredFields = enumListConstantClass.getDeclaredFields();
        for (Field declaredField : declaredFields) {
            if (declaredField.isAnnotationPresent(EnumInterface.class)) {
                List<EnumListConstant.EnumClass> enumClassList = Lists.newArrayList();
                EnumInterface enumInterface = declaredField.getAnnotation(EnumInterface.class);
                String enumName = enumInterface.value();
                declaredField.setAccessible(true);
                Object valueObj = null;
                try {
                    valueObj = declaredField.get(enumListConstant);
                } catch (IllegalAccessException e) {
                    throw new RuntimeException(e);
                }
                if (valueObj instanceof String) {
                    String valueStr = valueObj.toString();
                    String[] split = valueStr.split(",");
                    for (int i = 0; i < split.length; i++) {
                        EnumListConstant.EnumClass enumClass = new EnumListConstant.EnumClass();
                        enumClass.setValue(i);
                        enumClass.setLabel(split[i]);
                        enumClassList.add(enumClass);
                        enumMap.put(enumName, enumClassList);
                    }
                } else if (valueObj instanceof Collection) {
                    List<String> arrayList = new ArrayList<>((Collection)valueObj);
                    for (int i = 0; i < arrayList.size(); i++) {
                        EnumListConstant.EnumClass enumClass = new EnumListConstant.EnumClass();
                        enumClass.setValue(i);
                        enumClass.setLabel(arrayList.get(i));
                        enumClassList.add(enumClass);
                        enumMap.put(enumName, enumClassList);
                    }
                }
            }

        }
    }

}
